package com.chain.chl.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chain.chl.R;
import com.chain.chl.model.card.Card;
import com.chain.chl.widget.Navigator;
import com.facebook.drawee.view.SimpleDraweeView;
import com.just.agentweb.AbsAgentWebSettings;
import com.just.agentweb.AgentWeb;
import com.just.agentweb.AgentWebSettingsImpl;
import com.just.agentweb.IAgentWebSettings;
import com.just.agentweb.IEventHandler;
import com.just.agentweb.IWebLayout;

import java.io.Serializable;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by baoyunfeng on 2018/6/8.
 */

public class WebActivity extends AppCompatActivity {

    private static final String TAG = "WebActivity";

    private static String INTENT_CARD = "card";
    private Card preCard;

    @BindView(R.id.rootView)
    LinearLayout linearLayout;


    public static void open(Context context, Card card){
        Intent intent = new Intent(context,WebActivity.class);
        intent.putExtra(INTENT_CARD, (Serializable) card);
        context.startActivity(intent);
    }

    @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        ButterKnife.bind(this);
        preCard = (Card)getIntent().getSerializableExtra(INTENT_CARD);

        initViews();
    }

    private void initViews(){
        AgentWeb mAgentWeb = AgentWeb.with(this)
                .setAgentWebParent(linearLayout, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
                .useDefaultIndicator()
                .setAgentWebWebSettings(new IAgentWebSettings() {
                    @Override
                    public IAgentWebSettings toSetting(WebView webView) {

                        webView.getSettings().setJavaScriptEnabled(true);
                        webView.getSettings().setSupportZoom(true);
                        webView.getSettings().setBuiltInZoomControls(false);
                        webView.getSettings().setLoadWithOverviewMode(true);
                        webView.getSettings().setUseWideViewPort(true);
                        webView.getSettings().setLayoutAlgorithm(android.webkit.WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
                        webView.getSettings().setMinimumFontSize(14);

                        return this;
                    }

                    @Override
                    public WebSettings getWebSettings() {
                        return null;
                    }
                })
                .createAgentWeb()
                .ready()
                .go(preCard.getUrl());

    }

}
